home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / fonts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.1 KB  |  310 lines

  1. /*
  2. ** Copyright 1994, Silicon Graphics, Inc.
  3. ** All Rights Reserved.
  4. **
  5. ** This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6. ** the contents of this file may not be disclosed to third parties, copied or
  7. ** duplicated in any form, in whole or in part, without the prior written
  8. ** permission of Silicon Graphics, Inc.
  9. **
  10. ** RESTRICTED RIGHTS LEGEND:
  11. ** Use, duplication or disclosure by the Government is subject to restrictions
  12. ** as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13. ** and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14. ** successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15. ** rights reserved under the Copyright Laws of the United States.
  16. **
  17. */
  18. #include <X11/Xlib.h>
  19. #include <X11/Xutil.h>
  20. #include <GL/glx.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <X11/keysym.h>
  24. #include <string.h>
  25.  
  26. static int RGBattributes_DB[] = {
  27.     GLX_RGBA,
  28.     GLX_RED_SIZE, 1,
  29.     GLX_GREEN_SIZE, 1,
  30.     GLX_BLUE_SIZE, 1,
  31.     GLX_DOUBLEBUFFER,
  32.     None,
  33. };
  34.  
  35. static int CIattributes_DB[] = {
  36.     GLX_DOUBLEBUFFER,
  37.     None,
  38. };
  39.  
  40. enum {
  41.     BLACK = 0,
  42.     RED,
  43.     GREEN,
  44.     YELLOW,
  45.     BLUE,
  46.     MAGENTA,
  47.     CYAN,
  48.     WHITE
  49. };
  50.  
  51. #define COLOR_OFFSET_1    16
  52. #define COLOR_OFFSET_2    32
  53.  
  54.  
  55. static float rgbMap[][3] = {
  56.     {0, 0, 0},
  57.     {1, 0, 0},
  58.     {0, 1, 0},
  59.     {1, 1, 0},
  60.     {0, 0, 1},
  61.     {1, 0, 1},
  62.     {0, 1, 1},
  63.     {1, 1, 1}
  64. };
  65.  
  66. static long W = 300, H = 300;
  67. static Display *dpy;
  68. static Window window;
  69. static Colormap cmap;
  70. int doubleBuf = 1;
  71. int fontToggle = 0;
  72. GLenum drawBuffer = GL_FRONT;
  73. static long rgb;
  74. static long ci1 = BLUE, ci2 = GREEN;
  75.  
  76.  
  77. static LoadGLFont(GLuint *pBase, GLsizei *pCount)
  78. {
  79.     XFontStruct *fontInfo;
  80.     Font id;
  81.     int first, last;
  82.  
  83.     static char pattern1[] = "-adobe-courier-bold-o-normal--18-*-*-*-*-*-*-*";
  84.     static char pattern2[] = "-adobe-helvetica-*-o-normal-*-18-*-*-*-*-*-*-*";
  85.     static char pattern3[] = "-sgi-screen-bold-*-normal-*-18-*-*-*-*-*-*-*";
  86.     static char pattern4[] = "-sgi-rock-*-*-*-*-18-*-*-*-*-*-*-*";
  87.  
  88.     fontToggle = fontToggle%4;
  89.     switch (fontToggle) {
  90.     case 0: fontInfo = XLoadQueryFont(dpy, pattern1);
  91.         break;
  92.     case 1: fontInfo = XLoadQueryFont(dpy, pattern2);
  93.         break;
  94.     case 2: fontInfo = XLoadQueryFont(dpy, pattern3);
  95.         break;
  96.     case 3: fontInfo = XLoadQueryFont(dpy, pattern4);
  97.         break;
  98.     }
  99.  
  100.     id = fontInfo->fid;
  101.     first = (int)fontInfo->min_char_or_byte2;
  102.     last = (int)fontInfo->max_char_or_byte2;
  103.     *pCount = last-first+1;
  104.  
  105.  
  106.     *pBase = glGenLists(last+1);
  107.     if (*pBase == 0) {
  108.         return 0;
  109.     }
  110.     glXUseXFont(id, first, *pCount, (int)(*pBase+first));
  111. }
  112.  
  113.  
  114. static void Usage(void)
  115. {
  116.     printf("Usage: font [-c]\n");
  117.     printf("   -c:  Run in color index mode\n");
  118.     printf("   f key rotates through fonts \n");
  119.     exit(-1);
  120. }
  121.  
  122. static void DoDisplay(void)
  123. {
  124.     GLfloat y = 5.0;
  125.     GLuint base;
  126.     GLsizei count;
  127.  
  128.     LoadGLFont(&base, &count);
  129.     glListBase(base);
  130.  
  131.     glLoadIdentity();
  132.     glOrtho(0.0, W, 0.0, H, -0.5, 1000.0);
  133.  
  134.     glDrawBuffer(GL_FRONT);
  135.     glClearColor(0.0, 0.0, 0.0, 0.0);
  136.     glClearIndex(0);
  137.     glClear(GL_COLOR_BUFFER_BIT);
  138.  
  139.     glRasterPos2f(10.0, y);
  140.     glCallLists(26, GL_UNSIGNED_BYTE, 
  141.     (unsigned char *)"abcdefghijklmnopqrstuvwxyz");
  142.     y += 20.0;
  143.     glRasterPos2f(10.0, y);
  144.     glCallLists(26, GL_UNSIGNED_BYTE, 
  145.     (unsigned char *)"abcdefghijklmnopqrstuvwxyz");
  146.     y += 20.0;
  147.     glRasterPos2f(10.0, y);
  148.     glCallLists(26, GL_UNSIGNED_BYTE, 
  149.     (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  150.     y += 20.0;
  151.     glRasterPos2f(10.0, y);
  152.     glCallLists(26, GL_UNSIGNED_BYTE, 
  153.     (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  154.     y += 20.0;
  155.     glRasterPos2f(10.0, y);
  156.     glCallLists(10, GL_UNSIGNED_BYTE, "0123456789"); 
  157.     y += 20.0;
  158.     glRasterPos2f(10.0, y);
  159.     glCallLists(10, GL_UNSIGNED_BYTE, "0123456789"); 
  160.  
  161.     y += 20.0;
  162.     glRasterPos2f(10.0, y);
  163.     glCallLists(25, GL_UNSIGNED_BYTE, 
  164.     (unsigned char *)"N O P Q R S T U V W X Y Z");
  165.     y += 20.0;
  166.     glRasterPos2f(10.0, y);
  167.     glCallLists(25, GL_UNSIGNED_BYTE, 
  168.     (unsigned char *)"! @ # $ % ^ & * ( ) _ + }");
  169.     glXWaitGL();
  170.  
  171.     glFlush();
  172.     glDeleteLists(base, count);
  173. }
  174.  
  175. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  176. {
  177.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  178.     return GL_TRUE;
  179.     }
  180.     return GL_FALSE;
  181. }
  182.  
  183.  
  184. int main(long argc, char** argv)
  185. {
  186.     XVisualInfo *vi;
  187.     XSetWindowAttributes swa;
  188.     GLXContext cx;
  189.     XEvent event;
  190.     GLboolean needDisplay;
  191.     int i;
  192.  
  193.     rgb = 1;
  194.     doubleBuf = 1;
  195.     for (i = 1; i < argc; i++) {
  196.         if (argv[i][0] == '-') {
  197.             switch (argv[i][1]) {
  198.               case 'c':
  199.                 rgb = 0;
  200.                 break;
  201.               default:
  202.                 Usage();
  203.             }
  204.         } else {
  205.             Usage();
  206.         }
  207.     }
  208.  
  209.     dpy = XOpenDisplay(0);
  210.     if (!dpy) {
  211.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  212.     return -1;
  213.     }
  214.  
  215.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  216.              (rgb ? RGBattributes_DB : CIattributes_DB));
  217.     if (!vi) {
  218.     fprintf(stderr, "No appropriate visual on \"%s\"\n",
  219.         getenv("DISPLAY"));
  220.     return -1;
  221.     }
  222.  
  223.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  224.                rgb ? AllocNone : AllocAll);
  225.     if (!rgb) {
  226.         XColor buf;
  227.         int i;
  228.  
  229.         buf.flags = DoRed | DoGreen | DoBlue;
  230.  
  231.         /* Init color map */
  232.         for (i=0; i<16; i++) {
  233.             buf.pixel = i;
  234.             buf.blue = (i & 4) ? 65535 : 0;
  235.             buf.green = (i & 2) ? 65535 : 0;
  236.             buf.red = (i & 1) ? 65535 : 0;
  237.             if (i > 8) {
  238.                 buf.red /= 2;
  239.                 buf.green /= 2;
  240.                 buf.blue /= 2;
  241.             }
  242.             XStoreColor(dpy, cmap, &buf);
  243.         }
  244.     }
  245.  
  246.  
  247.     swa.border_pixel = 0;
  248.     swa.colormap = cmap;
  249.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  250.     | KeyReleaseMask;
  251.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  252.                W, H,
  253.                0, vi->depth, InputOutput, vi->visual,
  254.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  255.     XSetWMColormapWindows(dpy, window, &window, 1);
  256.     XStoreName(dpy, window, "Fonts Test (optimized)");
  257.     XMapWindow(dpy, window);
  258.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  259.  
  260.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  261.     if (!glXMakeCurrent(dpy, window, cx)) {
  262.     fprintf(stderr, "Can't make window current to context\n");
  263.     return -1;
  264.     }
  265.  
  266.  
  267.  
  268.     needDisplay = GL_TRUE;
  269.     for (;;) {
  270.     do {
  271.         XNextEvent(dpy, &event);
  272.         switch (event.type) {
  273.           case Expose:
  274.         needDisplay = GL_TRUE;
  275.         break;
  276.           case ConfigureNotify:
  277.         W = event.xconfigure.width;
  278.         H = event.xconfigure.height;
  279.         needDisplay = GL_TRUE;
  280.         break;
  281.           case KeyPress:
  282.         {
  283.             char buf[100];
  284.             int rv;
  285.             KeySym ks;
  286.  
  287.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  288.             switch (ks) {
  289.               case XK_f:
  290.             fontToggle ++;
  291.             needDisplay = GL_TRUE;
  292.             break;
  293.               case XK_r:
  294.             needDisplay = GL_TRUE;
  295.             break;
  296.               case XK_Escape:
  297.             return 0;
  298.             }
  299.         }
  300.         break;
  301.         }
  302.     } while (XPending(dpy) != 0);
  303.  
  304.     if (needDisplay) {
  305.         needDisplay = GL_FALSE;
  306.         DoDisplay();
  307.     }
  308.     }
  309. }
  310.